package com.yantong.controller;

import com.yantong.pojo.CentreIndustry;
import com.yantong.service.CentreIndustryService;
import com.yantong.annotation.Log;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 商户行业CentreInDustryController
 *
 * @author huangshuai-728982210@qq.com
 * @date 2021/1/19 19:20
 */
@Controller
@RequestMapping("/centre/centreIndustry")
public class CentreInDustryController extends BaseController {
	@Autowired
	private CentreIndustryService centreIndustryService;
	@Autowired
	private RoleService roleService;
	private static final String prefix = "centre/centreIndustry";


	/**
	 * 商户行业页面
	 *
	 * @return
	 */
	@GetMapping()
	@RequiresPermissions("centre:centreIndustry:centreIndustry")
	public String centreInDustry(Model model) {

		return prefix + "/centreIndustry";
	}

	/**
	 * 商户行业列表数据
	 *
	 * @param centreIndustry
	 * @param offset
	 * @param limit
	 * @return
	 */
	@GetMapping("/list")
	@RequiresPermissions("centre:centreIndustry:centreIndustry")
	@ResponseBody
	public PageUtils<CentreIndustry> listCentreInDustry(CentreIndustry centreIndustry, int offset, int limit) {
		return centreIndustryService.listCentreIndustry(centreIndustry, offset, limit);
	}

	/**
	 * 商户行业编辑页面
	 * @param centreIndustryId
	 * @param model
	 * @return
	 */
	@GetMapping("/edit/{centreIndustryId}")
	@RequiresPermissions("centre:centreIndustry:edit")
	public String edit(@PathVariable Long centreIndustryId, Model model) {
		model.addAttribute("centreIndustry", centreIndustryService.getCentreIndustryById(centreIndustryId));
		return prefix + "/edit";
	}

	/**
	 * 商户行业编辑操作
	 * @param centreIndustry
	 * @return
	 */
	@Log(module = "商户行业管理", operation = "编辑商户行业")
	@ResponseBody
	@RequiresPermissions("centre:centreIndustry:edit")
	@PostMapping("/update")
	public ResultVo update(CentreIndustry centreIndustry) {
		if (centreIndustry == null || centreIndustry.getId() == null) {
			return ResultVo.error();
		}
		if (centreIndustryService.updateCentreIndustry(centreIndustry) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	@RequiresPermissions("centre:centreIndustry:add")
	@GetMapping("/add")
	public String add(Model model) {
		model.addAttribute("roles", roleService.listByAll());
		return prefix +"/add";
	}


	@Log(module = "商户行业管理", operation = "添加商户行业")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("centre:centreIndustry:add")
	public ResultVo saveRate(CentreIndustry centreIndustry) {
		if (centreIndustry == null) {
			return ResultVo.error();
		}
		if (centreIndustryService.saveCentreIndustry(centreIndustry) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

}
